package com.example.personalaccount.controller;

import com.example.personalaccount.dto.JwtRequest;
import com.example.personalaccount.dto.JwtResponse;
import com.example.personalaccount.dto.RegisterRequest;
import com.example.personalaccount.dto.UserInformDto;
import com.example.personalaccount.entity.User;
import com.example.personalaccount.entity.UserInform;
import com.example.personalaccount.handler.SecurityGetUserUtil;
import com.example.personalaccount.service.AuthService;
import com.example.personalaccount.service.UserInformService;
import com.example.personalaccount.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller
@RequestMapping("/")
@ResponseBody
@Tag(name = "用户管理")
public class IndexController {

    @Autowired
    private AuthService authService;

    @Autowired
    private UserInformService userInformService;

    @Autowired
    private UserService userService;


    @PostMapping("/login")
    @Operation(summary = "登录")
    public JwtResponse login(@RequestBody JwtRequest request) {
        if (request != null) {
            JwtResponse login = authService.login(request);
            if (login != null) {

                return login;
            }
            throw new RuntimeException("the password or username is error");
        }
        throw new IllegalArgumentException("haven't this user");
    }

    @PostMapping("/register")
    @Operation(summary = "注册")
    public JwtResponse register(@RequestBody RegisterRequest request) {
        JwtResponse register = authService.register(request);
        if (register == null) {
            throw new RuntimeException("register failed");
        }
        return register;
    }


    @PreAuthorize("hasAnyRole('ADMIN','USER')")
    @PostMapping("/changePassword/{password}")
    @Operation(summary = "更改密码")
    public boolean changePassword(@RequestBody JwtRequest request, @PathVariable String password) {
        if (request != null) {
            return authService.changePassword(request, password);
        }
        throw new IllegalArgumentException("it is over stack data");
    }

    @PreAuthorize("hasAnyRole('ADMIN','USER')")
    @Operation(summary = "获取用户信息 如果用户设置不可见则返回null")
    @GetMapping("/userInfo/{id}")
    public UserInform get(@PathVariable long id) {
        UserInform byUserId = userInformService.getByUserId(id);
        return byUserId.isStatus()?byUserId:null;
    }

    @PreAuthorize("hasAnyRole('ADMIN')")
    @Operation(summary = "管理获取用户信息")
    @GetMapping("/userInfoAdmin/{id}")
    public UserInform getByAdmin(@PathVariable long id) {
        return userInformService.getByUserId(id);
    }


    @PreAuthorize("hasRole('ADMIN')")
    @Operation(summary = "恢复初始密码")
    @GetMapping("/recover/{id}")
    public boolean recover(@PathVariable long id) {
        return authService.recoverCode(id);
    }

    @PreAuthorize("hasAnyRole('ADMIN','USER')")
    @Operation(summary = "更新账户信息")
    @PostMapping("/user/update")
    public User update (@RequestBody User user){
        return userService.update(user);
    }

    @PreAuthorize("hasAnyRole('ADMIN','USER')")
    @Operation(summary = "更改用户名")
    @PostMapping("/userInform/updateUsername/{username}")
    public UserInform updateInformUsername (@PathVariable String username)
    {
       return userInformService.changeUsername(username, new SecurityGetUserUtil().getUser().getId());
    }

    @PreAuthorize("hasAnyRole('ADMIN','USER')")
    @Operation(summary = "更改用户名")
    @PostMapping("/userInform/update")
    public UserInform updateInform (@PathVariable UserInformDto userInformDto)
    {
        return userInformService.updateUserInform(userInformDto);
    }


}